﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Linq;
using AutoLoL.Entities;
using AutoLoL.Logic;

namespace AutoLoL.Dialogs
{
    public partial class ChampionDialog : Window
    {
        private System.Timers.Timer _filterTimer;
        private List<Champion> _selectedChampions;

        // Result
        public List<Champion> SelectedChampions { get; private set; }

        public ChampionDialog(Window owner, List<Champion> selected = null, bool isMultipleSelectAllowed = false, bool isSelectNoneAllowed = false)
        {
            InitializeComponent();
            Owner = owner;

            _selectedChampions = selected;
            ChampionSelectControl.IsMultipleSelectAllowed(isMultipleSelectAllowed);

            SaveButton.IsDefault = true;
            CancelButton.IsCancel = true;
            NoneButton.Visibility = (isSelectNoneAllowed ? Visibility.Visible : Visibility.Collapsed);

            _filterTimer = new System.Timers.Timer(100);
            _filterTimer.Elapsed += new ElapsedEventHandler(FilterTimer_Elapsed);
            _filterTimer.AutoReset = false;
            _filterTimer.Enabled = true;
            _filterTimer.Stop();

            FilterTextBox.Focus();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ChampionSelectControl.SetSelectedChampions(_selectedChampions);
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            SelectedChampions = ChampionSelectControl.GetSelected();
            Close();
        }

        private void NoneButton_Click(object sender, RoutedEventArgs e)
        {
            SelectedChampions = new List<Champion>();
            Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            SelectedChampions = null;
            Close();
        }

        private void SetFilter()
        {
            string[] filters = FilterTextBox.Text.ToLower().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
            ChampionSelectControl.SetFilter(filters);
        }

        private void FilterTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                SetFilter();
            }
        }

        private void FilterTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Dispatcher.BeginInvoke((ThreadStart)(() =>
            {
                SetFilter();
            }));
        }

        private void FilterTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            _filterTimer.Stop();
            _filterTimer.Start();
        }

        private void ChampionSelectControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            SelectedChampions = ChampionSelectControl.GetSelected();
            Close();
        }
    }
}
